"use strict";
const Index = {
	verfify: function() {
		
		var ts = Date.parse(new Date()) / 1000;
		$('#codeImg').attr("src", CONTEXTPATH_PROJECT+"/index/login/verify?id=" + ts);
	},
	validate: function() {
		let _this = this;
		let user_Boolean = false;
		let password_Boolean = false;
		let pomis_Boolean = false;

		$('#username').blur(function() {
			var user_img = $(this).parent("div").find("img");
			if((/^[\u4e00-\u9fa5a-zA-Z0-9]{4,20}$/).test($("#username").val())) {
				$('#pomis_hint').text("");
				user_img.attr("src", CONTEXTPATH_PROJECT + "/static/img/username_h.png");
				$(this).css('border', '1px #bababa solid');
				user_Boolean = true;
			} else if($("#username").val() == "") {
				$('#pomis_hint').html("用户名不能为空").css("color", "#ff0000");
				$(this).css('border', '1px #ff0000 solid');
				user_img.attr("src", CONTEXTPATH_PROJECT + "/static/img/username.png");
				user_Boolean = false;
			} else {
				$('#pomis_hint').html("用户名输入有误").css("color", "#ff0000");
				$(this).css('border', '1px #ff0000 solid');
				user_img.attr("src", CONTEXTPATH_PROJECT + "/static/img/username.png");
				user_Boolean = false;
			}

		});
		$('#password').blur(function() {
			var pwd_img = $(this).parent("div").find("img");
			if((/^[A-Za-z0-9]{6,16}$/).test($(this).val())) {
				$('#pomis_hint').html("");
				$(this).css('border', '1px #bababa solid');
				pwd_img.attr("src", CONTEXTPATH_PROJECT + "/static/img/pwd_h.png");
				password_Boolean = true;
			} else if($(this).val() == "") {
				$('#pomis_hint').html("密码不能为空").css("color", "#ff0000");
				$(this).css('border', '1px #ff0000 solid');
				pwd_img.attr("src", CONTEXTPATH_PROJECT + "/static/img/pwd.png");
				password_Boolean = false;
			} else {
				$('#pomis_hint').html("密码格式输入有误").css("color", "#ff0000");
				$(this).css('border', '1px #ff0000 solid');
				pwd_img.attr("src", CONTEXTPATH_PROJECT + "/static/img/pwd.png");
				password_Boolean = false;
			}

		});
		$('#verificationCode').blur(function() {
			var code_img = $(this).parent("div").find("img");
			if($(this).val() == "") {

				$('#pomis_hint').html("验证码不能为空").css("color", "#ff0000");
				$(this).css('border', '1px #ff0000 solid');
				code_img.attr("src", CONTEXTPATH_PROJECT + "/static/img/code.png");
				pomis_Boolean = false;
			} else {
				$('#pomis_hint').html("");
				$(this).css('border', '1px #bababa solid');
				code_img.attr("src", CONTEXTPATH_PROJECT + "/static/img/code_h.png");
				pomis_Boolean = true;
			}
		});
		$("#codeImg").click(function() {
			$("#codeImg").attr("src", CONTEXTPATH_PROJECT + "/verifCode?t=" + (new Date().getTime()));
		});
		$("#sub").click(function() {
			if(password_Boolean == true && user_Boolean == true && pomis_Boolean == true) {

				_this.loginAjax();

			} else {
				console.log(password_Boolean)
				console.log(user_Boolean)
				console.log(pomis_Boolean)
				layer.msg('信息填写不正确，请重新输入！');
				return false;
			}
		});
		$("input").bind("keydown", function(e) {
			if(e.which == 13) {
				var ei = $('input').index(this);
				console.log(ei)

				if((ei >= 0 && ei <= 2)) {
					$('input').eq(ei + 1).focus();
					if(ei == 2) {
						if(password_Boolean == true && user_Boolean == true && pomis_Boolean == true) {

							_this.loginAjax();

						} else {
							layer.msg('信息填写不正确，请重新输入！');
						}
					}
					return false
				}

			}
		});
	},
	loginAjax: function() {

		let _this = this;
		//var index = layer.load(0, {shade: false});
		loading({
			isShow: true,
			text: "正在登陆..."

		});

		var username = $("#username").val();
		var password = $("#password").val();
		var verificationCode = $("#verificationCode").val();
		$.ajax({
			type: "post",
			url: CONTEXTPATH_PROJECT + '/index/Login/toLogin',
			async: true,
			data: {
				"username": username,
				"password": password,
				"verificationCode": verificationCode
			},
			dataType: "json",
			success: function(res) {
				if(res == 0) {
					window.location.href = CONTEXTPATH_PROJECT ;
					
				}else if(res == 1){
					$(".loading").css('display', 'none');
					layer.msg("账号密码错误");
					_this.verfify();
				} else {
					$(".loading").css('display', 'none');
					layer.msg(res);
					
				}
			},
			error: function(res) {
				$(".loading").css('display', 'none');
				layer.msg("系统错误，请联系管理员！");
			}
		});
	}
};
Index.validate();